package org.android.pasos.data;

import java.util.ArrayList;
import java.util.List;

import org.android.pasos.Constants;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper {

    public static final String DB_NAME = "cliente";
    public static final String DB_TABLE = "tipoincidencias";
    public static final int DB_VERSION = 4;

    private static final String CLASSNAME = DBHelper.class.getSimpleName();
    private static final String[] COLS = new String[] { "_id", "descripcion"};    

    private SQLiteDatabase db;
    private final DBOpenHelper dbOpenHelper;

    // 
    // inner classes
    //

    public static class TIncidencia {

        public long id;
        public String descripcion;

        public TIncidencia() {
        }

        public TIncidencia(long id, String descripcion) {
			this.id = id;
			this.descripcion = descripcion;
		}

		@Override
		public String toString() {
			return "Tipo Incidencia [id=" + id +", descripcion=" + descripcion + "]";
		}
    }

    private static class DBOpenHelper extends SQLiteOpenHelper {

        private static final String DB_CREATE = "CREATE TABLE "
            + DBHelper.DB_TABLE
            + " (_id INTEGER PRIMARY KEY, descripcion TEXT);";

        public DBOpenHelper(final Context context) {
            super(context, DBHelper.DB_NAME, null, DBHelper.DB_VERSION);
        }

        @Override
        public void onCreate(final SQLiteDatabase db) {
            try {
                db.execSQL(DBOpenHelper.DB_CREATE);
            } catch (SQLException e) {
                Log.e(Constants.LOGTAG, DBHelper.CLASSNAME, e);
            }
        }

        @Override
        public void onOpen(final SQLiteDatabase db) {
            super.onOpen(db);
        }

        @Override
        public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + DBHelper.DB_TABLE);
            onCreate(db);
        }
    }

    //
    // end inner classes
    //

    public DBHelper(final Context context) {
        this.dbOpenHelper = new DBOpenHelper(context);
        establishDb();
    }

    private void establishDb() {
        if (this.db == null) {
            this.db = this.dbOpenHelper.getWritableDatabase();
        }
    }

    public void cleanup() {
        if (this.db != null) {
            this.db.close();
            this.db = null;
        }
    }

    public void insert(final TIncidencia tinci) {
        ContentValues values = new ContentValues();
        values.put("_id", tinci.id);
        values.put("descripcion", tinci.descripcion);
        try { 
        	this.db.insert(DBHelper.DB_TABLE, null, values);
        } catch (Exception e) {
			Log.d(Constants.LOGTAG, "No se pudo insertar el registro");
		}
    }

    public void update(final TIncidencia tinci) {
        ContentValues values = new ContentValues();
        values.put("descripcion", tinci.descripcion);
        this.db.update(DBHelper.DB_TABLE, values, "_id=" + tinci.id, null);
    }

    public void delete(final long id) {
        this.db.delete(DBHelper.DB_TABLE, "_id=" + id, null);
    }

    public TIncidencia get(final String id) {
        Cursor c = null;
        TIncidencia tinci = null;
        try {
            c = this.db.query(true, DBHelper.DB_TABLE, DBHelper.COLS, "_id = '" + id + "'", null, null, null, null,
                null);
            if (c.getCount() > 0) {
                c.moveToFirst();
                tinci = new TIncidencia();
                tinci.id = c.getLong(0);
                tinci.descripcion = c.getString(1);                
            }
        } catch (SQLException e) {
            Log.v(Constants.LOGTAG, DBHelper.CLASSNAME, e);
        } finally {
            if (c != null && !c.isClosed()) {
                c.close();
            }
        }
        return tinci;
    }

    public List<TIncidencia> getAll() {
        ArrayList<TIncidencia> ret = new ArrayList<TIncidencia>();
        Cursor c = null;
        try {
            c = this.db.query(DBHelper.DB_TABLE, DBHelper.COLS, null, null, null, null, null);
            int numRows = c.getCount();
            c.moveToFirst();
            for (int i = 0; i < numRows; ++i) {
            	TIncidencia tinci = new TIncidencia();
            	tinci.id = c.getLong(0);
            	tinci.descripcion = c.getString(1);
            	ret.add(tinci);
                c.moveToNext();
            }
        } catch (SQLException e) {
            Log.v(Constants.LOGTAG, DBHelper.CLASSNAME, e);
        } finally {
            if (c != null && !c.isClosed()) {
                c.close();
            }
        }
        return ret;
    }
}
